package com.java2007.hotel.controller.front;

import com.alibaba.fastjson.JSON;
import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.controller.BaseServlet;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.DetailOrder;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.DetailOrderService;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.service.FoodTypeService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理菜单详情的请求
 *
 * @author csl
 * @version V1.0
 * @date 2021/01/26
 **/
@WebServlet("/front/detailServlet")
public class FrontDetailServlet extends BaseServlet {
    private DetailOrderService detailOrderService = (DetailOrderService) BeanFactory.getBean(Constant.DETAIL_ORDER_SERVICE_IMPL);
    private FoodService foodService = (FoodService) BeanFactory.getBean(Constant.FOOD_SERVICE_IMPL);
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean(Constant.FOOD_TYPE_SERVICE_IMPL);
    /**
     * 增加一个菜单详情
     *
     * @param req
     * @param resp
     * @return
     */
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        String tempFoodId = req.getParameter("foodId");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        int foodId = Integer.parseInt(tempFoodId);
        Food food = null;
        try {
            food = foodService.selectFoodById(foodId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        Long price = user.getVipStatus() == 1? food.getVipPrice():food.getPrice();
        Date date = new Date();
        DetailOrder detailOrder = new DetailOrder(null, food.getId(), price, 1, price, date, date, null, food.getName());

        try {
            detailOrderService.addDetailOrder(detailOrder, user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        return "redirect:"+req.getContextPath()+"/front/detailServlet?method=selectAll";
    }

    /**
     * 查询未下单的菜单详情
     *
     * @param req
     * @param resp
     * @return
     */
    public String selectAll(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        List<DetailOrder> detailOrdersByUserIdAndUnOrder = null;
        try {
            detailOrdersByUserIdAndUnOrder = detailOrderService.selectDetailOrderByUserIdAndUnOrder(user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        req.setAttribute("detailOrdersByUserIdAndUnOrders",detailOrdersByUserIdAndUnOrder);


        //查询菜系
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        req.setAttribute("foodTypes",foodTypes);
        return "forward:"+"detail/clientCart.jsp";

    }


    /**
     * 修改未下单的菜单详情计数
     *
     * @param req
     * @param resp
     * @return
     */
    public String updateCount(HttpServletRequest req, HttpServletResponse resp){
        String tempDetailFoodId = req.getParameter("detailFoodId");
        String tempDetailFoodCount = req.getParameter("detailFoodCount");
        int detailFoodId = Integer.parseInt(tempDetailFoodId);
        int detailFoodCount = Integer.parseInt(tempDetailFoodCount);
        DetailOrder detailOrder = null;
        try {
            detailOrder = detailOrderService.selectById(detailFoodId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        Date date = new Date();
        try {
            detailOrderService.updateFoodCount(date,detailFoodCount,detailOrder.getFoodPrice()*detailFoodCount,detailFoodId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        return "redirect:"+req.getContextPath()+"/front/detailServlet?method=selectAll";
    }




    /**
     * 修改未下单的菜单详情计数
     *
     * @param req
     * @param resp
     * @return
     */
    public String updateCount2(HttpServletRequest req, HttpServletResponse resp){
        String tempDetailFoodId = req.getParameter("detailFoodId");
        String tempDetailFoodCount = req.getParameter("detailFoodCount");
        String bfoodTotalPrice = req.getParameter("bfoodTotalPrice");
        int detailFoodId = Integer.parseInt(tempDetailFoodId);
        int detailFoodCount = Integer.parseInt(tempDetailFoodCount);
        DetailOrder detailOrder = null;
        try {
            detailOrder = detailOrderService.selectById(detailFoodId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        Date date = new Date();
        try {
            detailOrderService.updateFoodCount(date,detailFoodCount,detailOrder.getFoodPrice()*detailFoodCount,detailFoodId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        List<DetailOrder> detailOrdersByUserIdAndUnOrders = null;
        try {
            detailOrdersByUserIdAndUnOrders = detailOrderService.selectDetailOrderByUserIdAndUnOrder(user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        Long totalAllPrice= 0L;
        for (DetailOrder detailOrdersByUserIdAndUnOrder : detailOrdersByUserIdAndUnOrders) {
            totalAllPrice += detailOrdersByUserIdAndUnOrder.getFoodTotalPrice();
        }
        Map<String, String> stringStringMap = new HashMap<String, String>();
        stringStringMap.put("totalAllPrice",(totalAllPrice/100.0)+"");
        stringStringMap.put("bfoodTotalPrice",bfoodTotalPrice);
        stringStringMap.put("dddxx",(detailOrder.getFoodPrice()*detailFoodCount/100.0)+"");
        String jsonString = JSON.toJSONString(stringStringMap);

        return "backText:"+jsonString;

    }

    /**
     * 删除一个订单详情
     * @param req
     * @param resp
     * @return
     */
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String tempDetailFoodId = req.getParameter("detailFoodId");
        int detailFoodId = Integer.parseInt(tempDetailFoodId);
        try {
            int i = detailOrderService.deleteDetailOrderById(detailFoodId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        return "redirect:"+req.getContextPath()+"/front/detailServlet?method=selectAll";

    }


    /**
     * 查询结算详情去结账页面
     * @param req
     * @param resp
     * @return
     */
    public String toAccount(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        String orderNumber = req.getParameter("orderNumber");
        req.setAttribute("orderNumber",orderNumber);
        List<DetailOrder> detailOrdersByUserIdAndUnOrder = null;
        try {
            detailOrdersByUserIdAndUnOrder = detailOrderService.selectDetailOrderByOrder(orderNumber);
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }
        req.setAttribute("detailOrdersByUserIdAndUnOrders",detailOrdersByUserIdAndUnOrder);


        //查询菜系
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "back:" + "服务器错误%"+"frontindex.jsp";
        }

        req.setAttribute("foodTypes",foodTypes);
        return "forward:"+"detail/clientOrderList.jsp";
    }


}
